<?php

namespace app\bbs\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Readarticle extends Controller
{
  public function index()
  {
    $RandStr = randstr(128); //生成一个128位的字符串
    Session::set('bbs_ReadArticle_RandStr', $RandStr);
    Session::set('bbs_ReadArticle_RandStr_success_time', time()); //生成随机数的时间
    $id = input('id', null);
    $me = Session::get('bbs_user_info');
    if (!$me) {
      $me['id'] = null;
    }
    if ($id) {
      if (Session::get('bbs_user_info')) {
        $editor_readonly = 'editor.readonly(false);'; //禁用ke编辑器
      } else {
        $editor_readonly = 'editor.readonly();'; //启用ke编辑器
      }
      $article = Db::table('bbs_article')
        ->alias('bbs_article')
        ->where(['bbs_article.id' => $id])
        ->join('user user', 'bbs_article.user_id = user.id')
        ->field('bbs_article.title,bbs_article.release_time,bbs_article.content,bbs_article.permission_to_read,bbs_article.start,bbs_article.user_id,user.user_name,user.nickname')
        ->find();
      switch ($article['start']) { // 判断文章状态 //状态 0正常 1 用户删除 2管理员删除 3隐藏登陆显示
        case '0':
          break;
        case '1':
          $msg = '文章已被删除';
          return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
          break;
        case '2':
          $msg = '文章已被删除';
          return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
          break;
        case '3':
          if (isset($me['email']) && isset($me['password']) && isset($me['user_name']) &&  isset($me['id'])) {
            $user = Db::table('user')
              ->where([
                'email' => $me['email'],
                'password' => $me['password'],
                'user_name' => $me['user_name'],
                'id' => $me['id'],
              ])
              ->find();
            if ($user) {
              if ($article['permission_to_read'] > $user['user_jurisdiction'] && $article['user_id'] != $user['id']) {
                $msg = '用户权限不足';
                return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
              }
            } else {
              $msg = '请重新登录';
              return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
            }
          } else {
            $msg = '登陆后查看文章';
            return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
          }
          break;
        default:
          $msg = '发生意外错误';
          return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
          # code...
          break;
      }

      if ($article['permission_to_read'] == 0) { } else { // 判断用户权限
        if (isset($me['email']) && isset($me['password']) && isset($me['user_name']) &&  isset($me['id'])) {
          $user = Db::table('user')
            ->where([
              'email' => $me['email'],
              'password' => $me['password'],
              'user_name' => $me['user_name'],
              'id' => $me['id'],
            ])
            ->find();
          if ($user) {
            if ($article['permission_to_read'] > $user['user_jurisdiction'] && $article['user_id'] != $user['id']) {
              $msg = '用户权限不足';
              return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
            }
          } else {
            $msg = '请重新登录';
            return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
          }
        } else {
          $msg = '登陆后查看文章';
          return $this->fetch('error', ['msg' => $msg, 'href_name' => '返回主页', 'href' => '/bbs/classification']);
        }
      }

      $user_info = Db::table('user')->where(['id' => $article['user_id']])->find();
      $head = head(null, 'layui-this');
      $comment = Db::table('bbs_comments')
        ->alias('bbs_comments')
        ->where(['bbs_comments.article_id' => $id])
        ->join('user user', 'bbs_comments.user_id = user.id')
        ->field('user.user_name,user.head_src,user.nickname,user.id,bbs_comments.comment_id,bbs_comments.user_id,bbs_comments.comments_time,bbs_comments.comments_body,bbs_comments.praise,bbs_comments.comment_me_id,bbs_comments.start,bbs_comments.id as comments_id')
        // ->limit(15)
        ->order('bbs_comments.id asc')
        ->select();
      foreach ($comment as $key => $value) {
        if ($value['comment_id']) {
          $comment_father = Db::table('bbs_comments') //获取父评论
            ->alias('bbs_comments')
            ->where('bbs_comments.id', 'eq', $value['comment_id'])
            ->join('user user', 'bbs_comments.user_id = user.id')
            ->find();
          switch ($comment_father['start']) {
            case '0':
              $comment[$key]['father'] = $comment_father;
              break;
            case '1':
              $comment[$key]['father'] = $comment_father;
              $comment[$key]['father']['comments_body'] = '<div class="locked">提示: <em>作者被禁止或删除 内容自动屏蔽</em></div>';
              break;
            case '2':
              $comment[$key]['father'] = $comment_father;
              break;
            default:
              $comment[$key]['father'] = $comment_father;
              break;
          }
        } else {
          $comment[$key]['father'] = '';
        }
        switch ($value['start']) {
          case '0':
            break;
          case '1':
            $comment[$key]['comments_body'] = '<div class="locked">提示: <em>作者被禁止或删除 内容自动屏蔽</em></div>';
            break;
          case '2':
            if ($value['user_id'] == $me['id'] || $article['user_id'] == $me['id']) { } else {
              $comment[$key]['comments_body'] = '<div class="locked">提示: <em>该评论设置了权限只有楼主或自己可看</em></div>';
            }
            break;
          default:
            # code...
            break;
        }
      }
      $web_settings = Db::table('bbs_settings')
        ->where(['id' => 1])
        ->find();
      $web_settings['rsa_public'] = str_replace("\n", '', $web_settings['rsa_public']);
      $web_settings['rsa_private'] = str_replace("\n", '', $web_settings['rsa_private']);
      Session::set($id . '_comment_count', count($comment)); //在session中存放当前文章的评论总量
      return $this->fetch('readarticle', ['head' => $head, 'article' => $article, 'user_info' => $user_info, 'comment' => $comment, 'me' => $me, 'web_settings' => $web_settings, 'editor_readonly' => $editor_readonly, 'get_id' => $id, 'bbs_ReadArticle_RandStr' => $RandStr]);
    } else {
      $Data['code'] = '10000';
      $Data['ErrorPosition'] = 'ReadarticleError';
      $Data['msg'] = '缺少文章id';
      return json($Data);
    }
  }
  public function comment_release()
  {
    $bbs_settings = Db::table('bbs_settings')
      ->where('id=1')
      ->field('rand_str_out_time,title')
      ->find();
    $checkbox = input('checkbox', 0) + 0;
    if ($checkbox > 2 || $checkbox == 1) {
      $checkbox = 0;
    }
    function normal_comments($bbs_settings, $checkbox) //正常评论
    {
      $id = input('id', null); // 文章id
      $title = rsa_decode(input('title', null));
      $Home_id = rsa_decode(input('Home_id', null)); //文章主id 不可用于插入数据
      $email = rsa_decode(input('email', null));
      $RandStr = input('RandStr', null);
      $comment = input('comment', null);
      $SessionRandStr = Session::get('bbs_ReadArticle_RandStr');
      $SessionRandStrSuccessTime = Session::get('bbs_ReadArticle_RandStr_success_time');
      if ($id &&  $title && $RandStr && $comment && $email) {
        if ($SessionRandStr) {
          if ($SessionRandStrSuccessTime + $bbs_settings['rand_str_out_time'] > time()) { //判断声称字符串的时间+数据库的rand_str_out_time 是否大雨当前时间是否为过期时间

            $BbsArticleCount = Db::table('bbs_article')
              ->where([
                'bbs_article.id' => $id,
                'bbs_article.title' => $title,
              ])
              ->alias('bbs_article')
              ->join('user user', 'bbs_article.user_id = user.id')
              ->field('bbs_article.comment_email_notification,bbs_article.are_comments_allowed,bbs_article.title,content,bbs_article.release_time,user.head_src,user.user_name,user.nickname')
              ->find();
            if ($BbsArticleCount['nickname']) {
              $UserName = $BbsArticleCount['nickname'];
            } else {
              $UserName = $BbsArticleCount['user_name'];
            }
            if ($BbsArticleCount && $BbsArticleCount['are_comments_allowed'] == 1) {
              $me = Session::get('bbs_user_info');
              $insert_time = date('Y-m-d H:i:s');
              if ($me['id'] && $me) {
                $comment = str_replace('<script>', '&lt;script&gt;', $comment);
                Db::table('bbs_comments')
                  ->insert([
                    'article_id' => $id,
                    'comment_id' => 0,
                    'user_id' => $me['id'],
                    'comments_body' => $comment,
                    'comments_time' => $insert_time,
                    'praise' => 0,
                    'comment_me_id' => 0,
                    'start' => $checkbox,
                  ]);

                $task = Db::table('bbs_task_list') #判断是否是 发布的任务
                  ->where([
                    'article_id' => $id,
                    'start' => 0,
                  ])
                  ->select();
                if ($task) {
                  foreach ($task as $key => $value) {
                    $start = Db::table('bbs_task_speed_of_progress_list')
                      ->where([
                        'task_id' => $value['id'],
                        'user_id' => $me['id'],
                        'start' => 1,
                      ])->find();
                    if ($start) {
                      $start = Db::table('bbs_task_speed_of_progress_list')
                        ->where([
                          'task_id' => $value['id'],
                          'user_id' => $me['id'],
                          'start' => 1,
                        ])->update([
                          'start' => 2,
                        ]);
                      break;
                    }
                  }
                }

                if ($BbsArticleCount['comment_email_notification'] === 1) {
                  $bbs_article_url = Request()->domain() . "bbs/readarticle/index?id=$id";
                  $body = <<<ETO
                                        <div id="contentDiv" onmouseover="getTop().stopPropagation(event);"
                                        onclick="getTop().preSwapLink(event, 'html', 'ZC1011-_PulQVfOkp~5IS1wQzecK97');"
                                        style="position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;"
                                        class="body">
                                        <div id="qm_con_body">
                                          <div id="mailContentContainer" class="qmbox qm_con_body_content qqmail_webmail_only" style="">

                                            <style type="text/css" media="all">
                                              .qmbox a:hover {
                                                color: red;
                                              }
                                          
                                              .qmbox a {
                                                text-decoration: none;
                                                color: #0088cc;
                                              }
                                          
                                              .qmbox a.primaryactionlink:link,
                                              .qmbox a.primaryactionlink:visited {
                                                background-color: #2585B2;
                                                color: #fff;
                                              }
                                          
                                              .qmbox a.primaryactionlink:hover,
                                              .qmbox a.primaryactionlink:active {
                                                background-color: #11729E !important;
                                                color: #fff !important;
                                              }
                                          
                                              /*
                                               @media only screen and (max-device-width: 480px) { 
                                                 .post { min-width: 700px !important; }
                                              }
                                            */
                                            </style>
                                          
                                            <table border="0" cellspacing="0" cellpadding="0" style="width: 100%; background: #DDDDDD;">
                                              <tbody>
                                                <tr>
                                                  <td>
                                                    <table border="0" cellspacing="0" cellpadding="0" align="center" class=""
                                                      style="width: 100%; padding: 10px">
                                                      <tbody>
                                                        <tr>
                                                          <td>
                                                            <div style="direction: ltr; max-width: 600px; margin: 0 auto; overflow: hidden;">
                                                              <table border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff" class="subscribe-wrapper"
                                                                style="width: 100%; background-color: #fff; text-align: left; max-width: 1024px; min-width: 320px; margin: 0 auto;">
                                                                <tbody>
                                                                  <tr>
                                                                    <td>
                                                                      <table border="0" cellspacing="0" cellpadding="0" height="8"
                                                                        background="https://s0.wp.com/i/emails/stripes.gif" class="subscribe-header-wrap"
                                                                        style="width: 100%; background-image: url(https://s0.wp.com/i/emails/stripes.gif); background-repeat: repeat-x; background-color: #43A4D0; height: 8px;">
                                                                        <tbody>
                                                                          <tr>
                                                                            <td></td>
                                                                          </tr>
                                                                        </tbody>
                                                                      </table>
                                                                      <table border="0" cellspacing="0" cellpadding="0" class="subscribe-header"
                                                                        style="width: 100%; color: #08c; font-size: 1.6em; background-color: #EFEFEF; border-bottom: 1px solid #DDD; margin: 0; padding: 0;">
                                                                        <tbody>
                                                                          <tr>
                                                                            <td>
                                                                              <h2 class="subscribe-title"
                                                                                style="margin: .4em 0 .3em; font-size: 1.8em; font-size: 16px!important; line-height: 1; font-weight: 400; color: #464646; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; margin: 5px 20px!important; padding: 0;">
                                                                                New post on <strong>$bbs_settings[title]</strong> </h2>
                                                                            </td>
                                                                            <td style="text-align: right;">
                                                                              <img border="0" class="head-avatar"
                                                                                src="https://ws3.sinaimg.cn/mw690/006wvbvUly1g4tfj2bsohj301e01eglh.jpg"
                                                                                width="32" height="32"
                                                                                style="vertical-align: middle; margin: 5px 20px 5px 0; vertical-align: middle;">
                                                                            </td>
                                                                          </tr>
                                                                        </tbody>
                                                                      </table>
                                                                      <table style="width: 100%" border="0" cellspacing="0" cellpadding="20"
                                                                        bgcolor="#ffffff">
                                                                        <tbody>
                                                                          <tr>
                                                                            <td>
                                                                              <table style="width: 100%" border="0" cellspacing="0" cellpadding="0">
                                                                                <tbody>
                                                                                  <tr>
                                                                                    <td valign="top" class="the-post">
                                                                                      <table style="width: 100%" border="0" cellspacing="0" cellpadding="0">
                                                                                        <tbody>
                                                                                          <tr>
                                                                                            <td
                                                                                              style="width: 60px !important; white-space: nowrap; vertical-align: top;">
                                                                                              <a href="https://blog.reimu.net/?author=1"
                                                                                                style="text-decoration: underline; color: #2585B2; display: block; margin-right: 10px;"
                                                                                                rel="noopener" target="_blank"><img border="0"
                                                                                                  src="$BbsArticleCount[head_src]"
                                                                                                  class="avatar avatar-50" height="50" width="50"></a>
                                                                                            </td>
                                                                                            <td>
                                                                                              <h2 class="post-title"
                                                                                                style="margin: .4em 0 .3em; font-size: 1.8em; font-size: 1.6em; color: #555; margin: 0; font-size: 20px;">
                                                                                                <a href="$bbs_article_url"
                                                                                                  style="text-decoration: underline; color: #2585B2; text-decoration: none !important;"
                                                                                                  rel="noopener" target="_blank">您的文章《$BbsArticleCount[title]》
                                                                                              <span style="color: #888;">by <a
                                                                                                  href="https://blog.reimu.net/?author=1"
                                                                                                  style="text-decoration: underline; color: #2585B2; color: #888 !important;"
                                                                                                  rel="noopener" target="_blank">$UserName</a> </span>
                                                                                            </td>
                                                                                          </tr>
                                                                                        </tbody>
                                                                                      </table>
                                                                                      <div class="post-content"
                                                                                        style="direction: ltr; margin-top: 1em; max-width: 560px;">
                                                                                        <p
                                                                                          style="direction: ltr; font-size: 14px; line-height: 1.4em; color: #444; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; margin: 0 0 1em;">
                                                                                            $BbsArticleCount[content]
                                                                                          </p>
                                                                                        <div style="direction: ltr; clear: both"></div>
                                                                                      </div>
                                          
                                                                                      <div class="meta"
                                                                                        style="direction: ltr; color: #999; font-size: .9em; margin-top: 4px; line-height: 160%; padding: 15px 0 15px; border-top: 1px solid #eee; border-bottom: 1px solid #eee; overflow: hidden">
                                                                                        <strong><a style="text-decoration: underline; color: #2585B2;"
                                                                                            href="https://blog.reimu.net/?author=1" rel="noopener"
                                                                                            target="_blank">$UserName</a></strong> | $BbsArticleCount[release_time] |
                                                                                        URL: <a style="text-decoration: underline; color: #2585B2;"
                                                                                          href="$bbs_article_url" rel="noopener"
                                                                                          target="_blank">$bbs_article_url</a> </div>
                                          
                                          
                                                                                      <p class="subscribe-action-links"
                                                                                        style="direction: ltr; font-size: 14px; line-height: 1.4em; color: #444; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; margin: 0 0 1em; font-size: 14px; color: #666; padding: 0; width: auto; padding-top: 1em; padding-bottom: 0em; margin-bottom: 0; margin-left: 0; padding-left: 0;">
                                                                                      </p>
                                                                                      <table class="auto-width" border="0" cellspacing="0" cellpadding="0"
                                                                                        style="width: 100%; width: auto">
                                                                                        <tbody>
                                                                                          <tr>
                                                                                            <td style="width: 10px;"><a
                                                                                                href="$bbs_article_url"
                                                                                                style="text-decoration: underline; color: #2585B2; -moz-border-radius: 10em; -webkit-border-radius: 10em; border-radius: 10em; border: 1px solid #11729E; text-decoration: none; color: #fff; text-shadow: 0 1px 0 #11729E; background-color: #2585B2; padding: 5px 15px; font-size: 16px; line-height: 1.4em; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-weight: normal; margin-left: 0; white-space: nowrap;"
                                                                                                rel="noopener" target="_blank">回复该评论</a></td>
                                                                                            <td>&nbsp;&nbsp;&nbsp;<a class="subscribe-action-link"
                                                                                                href="$bbs_article_url"
                                                                                                style="text-decoration: underline; color: #2585B2; text-decoration: underline"
                                                                                                rel="noopener" target="_blank">查看所有评论</a></td>
                                                                                          </tr>
                                                                                        </tbody>
                                                                                      </table>
                                                                                    </td>
                                                                                  </tr>
                                                                                </tbody>
                                                                              </table>
                                                                            </td>
                                                                          </tr>
                                                                        </tbody>
                                                                      </table>
                                                                      <table border="0" cellspacing="0" cellpadding="20" bgcolor="#efefef"
                                                                        class="subscribe-wrapper-sub"
                                                                        style="width: 100%; background-color: #efefef; text-align: left; border-top: 1px solid #ddd;">
                                                                        <tbody>
                                                                          <tr>
                                                                            <p
                                                                              style="direction: ltr; font-size: 14px; line-height: 1.4em; color: #444; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; margin: 0 0 1em; font-size: 12px; line-height: 1.4em; margin: 0px 0px 10px 0px;">
                                                                              <td valign="top" class="the-post">
                                                                              <table style="width: 100%" border="0" cellspacing="0" cellpadding="0">
                                                                                <tbody>
                                                                                  <tr>
                                                                                    <td style="width: 60px !important; white-space: nowrap; vertical-align: top;">
                                                                                      <a href="https://blog.reimu.net/?author=1"
                                                                                        style="text-decoration: underline; color: #2585B2; display: block; margin-right: 10px;" rel="noopener"
                                                                                        target="_blank"><img border="0" src="https://wx1.sinaimg.cn/large/006wvbvUly1g3dn2lgc4ij32yo1o0npf.jpg"
                                                                                          class="avatar avatar-50" height="50" width="50"></a>
                                                                                    </td>
                                                                                    <td>
                                                                                      <h2 class="post-title"
                                                                                        style="margin: .4em 0 .3em; font-size: 1.8em; font-size: 1.6em; color: #555; margin: 0; font-size: 20px;">
                                                                                      </h2>
                                                                                      <a href="http://localhostbbs/readarticle/index?id=32"
                                                                                        style="text-decoration: underline; color: #2585B2; text-decoration: none !important;" rel="noopener"
                                                                                        target="_blank"></a>您的文章《内网穿透》
                                                                                      <span style="color: #888;">by <a href="https://blog.reimu.net/?author=1"
                                                                                          style="text-decoration: underline; color: #2585B2; color: #888 !important;" rel="noopener"
                                                                                          target="_blank">1111</a> </span>
                                                                                    </td>
                                                                                  </tr>
                                                                                </tbody>
                                                                              </table>
                                                                              <div class="post-content" style="direction: ltr; margin-top: 1em; max-width: 560px;">
                                                                                <div style="direction: ltr; clear: both">
                                                                                  $comment
                                                                                </div>
                                                                              </div>
                                                                            </td>
                                                                            </p>
                                                                          </tr>
                                                                        </tbody>
                                                                      </table>
                                                                    </td>
                                                                  </tr>
                                                                </tbody>
                                                              </table>
                                                              <table border="0" cellspacing="0" cellpadding="0" height="3"
                                                                background="https://s0.wp.com/i/emails/stripes.gif" class="subscribe-footer-wrap"
                                                                style="width: 100%; background-image: url(https://s0.wp.com/i/emails/stripes.gif); background-repeat: repeat-x; background-color: #43A4D0; height: 3px;">
                                                                <tbody>
                                                                  <tr>
                                                                    <td></td>
                                                                  </tr>
                                                                </tbody>
                                                              </table>
                                                            </div>
                                                          </td>
                                                        </tr>
                                                      </tbody>
                                                    </table><br>
                                                  </td>
                                                </tr>
                                              </tbody>
                                            </table><img border="0"
                                              src="http://pixel.wp.com/b.gif?blog=105089382&amp;post=35316&amp;subd=blog.reimu.net&amp;ref=&amp;email=1&amp;email_o=jetpack&amp;host=jetpack.wordpress.com"
                                              width="1" height="1"><img border="0"
                                              src="http://pixel.wp.com/t.gif?email_name=email_subscription&amp;blog_id=105089382&amp;_ui=107370873&amp;email_id=fcaef12ce011586c155f99f4cbea2a23&amp;date_sent=2019-07-11&amp;domain=blog.reimu.net&amp;frequency=0&amp;digest=0&amp;has_html=1&amp;locale=en&amp;_en=wpcom_email_open&amp;browser_type=php-agent&amp;_aua=wpcom-tracks-client-v0.3&amp;_ul=juyaobin&amp;_ut=wpcom%3Auser_id&amp;blog_tz=8&amp;blog_lang=0&amp;user_lang=en&amp;_ts=1562810712491"
                                              width="1" height="1">
                                          
                                          
                                          
                                            <style type="text/css">
                                              .qmbox style,
                                              .qmbox script,
                                              .qmbox head,
                                              .qmbox link,
                                              .qmbox meta {
                                                display: none !important;
                                              }
                                            </style>
                                          </div>
                                        </div><!-- -->
                                        <style>
                                          #mailContentContainer .txt {
                                            height: auto;
                                          }
                                        </style>
                                        div>
ETO;
                  D_Mail('a', '202184199@qq.com',  $body);
                }
                if ($me['nickname']) {
                  $UserName = $me['nickname'];
                } else {
                  $UserName = $me['user_name'];
                }
                if ($Home_id) {
                  $louzhu = <<<ETO
                                    <em style="color:#FF9E3F">
                                            楼主
                                        </em>
ETO;
                } else {
                  $louzhu = <<<ETO
                                    <em style="color:#FF9E3F">
                                            楼主
                                        </em>
ETO;
                }
                $floor = Session::get($id . '_comment_count');
                $floor++;
                Session::set($id . '_comment_count', $floor); //在session中存放当前文章的评论总量
                $floor .= 'F';
                $eto['msg'] = <<<ETO
                            <li data-id="13">
                            <div class="global detail-about detail-about-reply">
                                <a class="global jie-user"> <img src="$me[head_src]" alt=""> 
                                    <cite>
                                        <i>$UserName</i>
                                        <em style="color:rgb(255, 0, 179)">
                                            我自己
                                        </em>
                                        $louzhu
                                    </cite>
                                </a>
                                <div class="global detail-hits">
                                    <span>$insert_time</span>
                                </div>
                                <i class="global iconfont icon-floor" title="最佳答案">$floor</i>
                            </div>
                            <div class="global detail-body jieda-body">
                                <p><span style="font-family:&quot;font-size:14px;background-color:#FFFFFF;">$comment</span><span style="font-family:&quot;font-size:14px;background-color:#FFFFFF;"></span></p>
                            </div>
                            <div class="global jieda-reply">
                                <span class="global jieda-zan" type="zan"><i class="global iconfont icon-zan"></i><em>0</em>
                                </span>
                                <!-- <div class="global jieda-admin">
                                    <span type="del"><a href="#"
                                            class="global layui-btn layui-btn-danger layui-btn-small">删除</a></span>
                                    <span class="global jieda-accept" type="accept">
                                        <a href="#" class="global layui-btn  layui-btn-small">采纳</a></span>
                                </div> -->
                            </div>
                        </li>
ETO;
                $eto['code'] = '1';
                return $eto;
              } else {
                $Data['code'] = '10009';
                $Data['ErrorPosition'] = 'ReadarticleError';
                $Data['msg'] = 'UserInfoError';
                return json($Data);
              }
            } else if ($BbsArticleCount['are_comments_allowed'] === 0) {
              $Data['code'] = '10018';
              $Data['ErrorPosition'] = 'ReadarticleError';
              $Data['msg'] = '当前文章不允许被评论';
              return json($Data);
            } else {
              $Data['code'] = '10008';
              $Data['ErrorPosition'] = 'ReadarticleError';
              $Data['msg'] = 'ArticleError';
              return json($Data);
            }
          } else {
            $Data['code'] = '10006';
            $Data['ErrorPosition'] = 'ReadarticleError';
            $Data['msg'] = 'RandStrOut';
            return json($Data);
          }
        } else {
          $Data['code'] = '10005';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '没有生成随机字符串 本次操作已被记录';
          error($Data['code'], $Data['msg'], 2, request()->controller() . '/' . request()->module() . '/' . request()->action());
          return json($Data);
        }
      } else {
        if (!$id) {
          $Data['code'] = '10001';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '缺少文章id';
          return json($Data);
        } else
            if (!$title) {
          $Data['code'] = '10002';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'title 解析失败';
          return json($Data);
        } else
            if (!$RandStr) {
          $Data['code'] = '10003';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '缺少RandStr';
          return json($Data);
        } else
            if (!$comment) {
          $Data['code'] = '10004';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '缺少comment';
          return json($Data);
        }
        if (!$email) {
          $Data['code'] = '10007';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'email解析失败';
          return json($Data);
        }
      }
    }
    function son_release($bbs_settings, $checkbox) //子评论
    {
      $article_id = rsa_decode(input('article_id', null));
      $comment_id = rsa_decode(input('comment_id', null));
      $RandStr = input('RandStr', null);
      $me_info = rsa_decode(input('me_info', null));
      $comment = input('comment', null);
      if ($article_id && $comment_id && $RandStr && $me_info &&  $comment) {
        $me_info = json_decode($me_info, true);
        $SessionRandStr = Session::get('bbs_ReadArticle_RandStr');
        $SessionRandStrSuccessTime = Session::get('bbs_ReadArticle_RandStr_success_time');
        if ($SessionRandStr) {
          if ($SessionRandStrSuccessTime + $bbs_settings['rand_str_out_time'] > time()) { //判断声称字符串的时间+数据库的rand_str_out_time 是否大雨当前时间是否为过期时间
            $user_info = Db::table('user')
              ->where($me_info)
              ->find();
            $insert_time = date('Y-m-d H:i:s');
            $comment_me_id =  Db::table('bbs_comments')->where(['article_id' => $article_id])->count() + 1;
            Db::table('bbs_comments')
              ->insert([
                'article_id' => $article_id,
                'comment_id' => $comment_id,
                'user_id' => $user_info['id'],
                'comments_body' => $comment,
                'comments_time' => $insert_time,
                'praise' => 0,
                'comment_me_id' => $comment_me_id,
                'start' => $checkbox, //状态 0正常 1封禁/删除 2隐藏
              ]);
            $me_home_floor = $comment_me_id . 'F'; //我的楼层的第几楼
            $father_comments = Db::table('bbs_comments') //获取父评论
              ->alias('bbs_comments')
              ->where(['bbs_comments.id' => $comment_id])
              ->join('user user', 'bbs_comments.user_id = user.id')
              ->find();
            $bbs_article = Db::table('bbs_article') //获取父评论
              ->alias('bbs_article')
              ->where(['bbs_article.id' => $article_id])
              ->find();
            if ($user_info['nickname']) {
              $user_name_html = $user_info['nickname']; //用户信息的html代码主要显示是不是楼主和名字
            } else {
              $user_name_html = $user_info['user_name']; //用户信息的html代码主要显示是不是楼主和名字
            }
            if ($user_info['id'] == $bbs_article['user_id']) {
              $user_name_html .= <<<ETO
                            <em style="color:#FF9E3F">
                            楼主
                            </em>
                            <em style="color:rgb(255, 0, 179)">
                            我自己
                            </em>
ETO;
            } else {
              $user_name_html .= <<<ETO
                            <em style="color:rgb(255, 0, 179)">
                            我自己
                            </em>
ETO;
            }
            $data['code'] = 1;
            $data['msg'] =
              <<<eto

                        <div style=" background: rgb(237, 251, 255)" class="quote">
                                <blockquote>
                                    <font size="2">
                                        <font color="#999999">
                                            $father_comments[user_name] 发表于 $father_comments[comments_time]
                                        </font>
                                    </font>
                                    <li>
                                        <span style="float: left;">
                                            $father_comments[comments_body]
                                        </span>
                                        <i class="iconfont icon-floor-son" style="float: right;"
                                            title="$father_comments[comment_me_id]F">$father_comments[comment_me_id]F</i>
                                    </li>
                                </blockquote>
                            </div>
    
                            <div>
                                <li>
                                    <div style="margin:auto;width: 90%;">
                                        <div class="detail-about detail-about-reply">
                                            <a class="jie-user"> <img src="$user_info[head_src]" alt=""> <cite>
                                                $user_name_html
                                                </cite>
                                            </a>
                                            <div class="detail-hits">
                                                <span>$insert_time</span>
                                            </div>
                                            <i class="iconfont icon-floor" title="$me_home_floor">$me_home_floor</i>
                                        </div>
                                        <div class="detail-body jieda-body">
                                            <p>$comment</p>
                                        </div>
                                    </div>
                                    <div class="jieda-reply">
                                        <span class="jieda-zan" type="zan"><i
                                                class="iconfont icon-zan"></i><em>0</em>
                                        </span>
                                        
                                    </div>
                                </li>
                            </div>
eto;
            return $data;
          } else {
            $Data['code'] = '10006';
            $Data['ErrorPosition'] = 'ReadarticleError';
            $Data['msg'] = 'RandStrOut';
            return json($Data);
          }
        } else {
          $Data['code'] = '10005';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '没有生成随机字符串 本次操作已被记录';
          error($Data['code'], $Data['msg'], 2, request()->controller() . '/' . request()->module() . '/' . request()->action());
          return json($Data);
        }
      } else {
        if (!$article_id) {
          $Data['code'] = '10010';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'article_id 参数丢失';
          return json($Data);
        }
        if (!$comment_id) {
          $Data['code'] = '10011';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'comment_id 参数丢失';
          return json($Data);
        }
        if (!$RandStr) {
          $Data['code'] = '10012';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'RandStr 参数丢失';
          return json($Data);
        }
        if (!$me_info) {
          $Data['code'] = '10013';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'me_info 参数丢失';
          return json($Data);
        }
        if (!$comment) {
          $Data['code'] = '10014';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = 'comment 参数丢失';
          return json($Data);
        }
      }
    }
    $_POST['son'] = $_POST['son'];
    if ($_POST['son'] == 'true') {
      return son_release($bbs_settings, $checkbox); //子评论
    } else {
      return normal_comments($bbs_settings, $checkbox); //正常评论
    }
  }
  public function reply_son() // 回复子楼
  {
    $article_id = $_GET['article_id'];
    $comment_id = $_GET['comment_id'];
    /* 判断是否存在参数 */
    $RandStr = randstr(128); //生成一个128位的字符串
    Session::set('bbs_ReadArticle_RandStr', $RandStr); // 设置随机字符串
    Session::set('bbs_ReadArticle_RandStr_success_time', time()); //生成随机数的时间
    $me = Session::get('bbs_user_info');
    if ($me['email'] && $me) { } else {/* 判断用户是否登陆 */
      $Data['code'] = '10009';
      $Data['ErrorPosition'] = 'ReadarticleError';
      $Data['msg'] = 'UserInfoError';
      return json($Data);
    }
    $me = rsa_encode(json_encode($me));
    $web_settings = Db::table('bbs_settings')
      ->where(['id' => 1])
      ->find();
    $comment = Db::table('bbs_comments')
      ->alias('bbs_comments')
      ->where([
        'bbs_comments.id' => $comment_id,
        'bbs_comments.article_id' => $article_id,
      ])
      ->join('user', "user.id = bbs_comments.user_id")
      ->field('bbs_comments.comments_body,bbs_comments.start,user.nickname,user.user_name')
      ->find();

    if ($comment) {
      switch ($comment['start']) {
        case '1':
          $Data['code'] = '10016';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '提示: 作者被禁止或删除 内容自动屏蔽';
          return json($Data);

          break;
        case '2':
          $Data['code'] = '10017';
          $Data['ErrorPosition'] = 'ReadarticleError';
          $Data['msg'] = '提示: 该评论设置了权限只有楼主或自己可看';
          return json($Data);

          break;

        default:
          # code...
          break;
      }
      if ($comment['nickname']) {
        $UserName = $comment['nickname'];
      } else {
        $UserName = $comment['user_name'];
      }
      $article_id = rsa_encode($article_id);
      $comment_id = rsa_encode($comment_id);
      return <<<ETO
        <style type="text/css">
            button {  
               width: 200px;  
               padding:8px;  
               background-color: #428bca;  
               border-color: #357ebd;  
               color: #fff;  
               -moz-border-radius: 10px;  
               -webkit-border-radius: 10px;  
               border-radius: 10px; /* future proofing */  
               -khtml-border-radius: 10px; /* for old Konqueror browsers */  
               text-align: center;  
               vertical-align: middle;  
               border: 1px solid transparent;  
               font-weight: 900;  
               font-size:125%  
             }  
             .mod{
                display: flex;
                align-items: center;/*垂直居中*/
            }
        </style>
        <script charset="utf-8" src="/public/bbs/module/kindeditor/kindeditor-all.js"></script>
        <script charset="utf-8" src="/public/bbs/module/kindeditor/lang/zh-CN.js"></script>
        <script src="/public/jquery/jquery-3.3.1.min.js"></script>
        <script src="/public/layer/layer.js"></script>
        <link rel="stylesheet" href="/public/bbs/readarticle/inserthtml.com.radios.css">
        <link rel="stylesheet" href="/public/bbs/readarticle/style.css">
        <span id="toName" style="color: blue">
            @ $UserName 说
        </span>
            <div style='max-height: 300px;overflow: hidden; text-overflow:ellipsis; white-space: nowrap;'> $comment[comments_body] </div>
                <div class="layui-form layui-form-pane">
                <div class="layui-form-item layui-form-text">
                    <div class="layui-input-block">
                        <textarea id="editor_id_1" name="content"
                            placeholder="$web_settings[bbs_readarticle_comment_text]"
                            class="layui-textarea fly-editor" style="height: 150px;"
                            style="width:700px;height:300px;"
                            placeholder="啊啊">$web_settings[bbs_readarticle_comment_text]</textarea>
                    </div>
                </div>
                <div class="layui-form-item" style='float: left;'>
                    <button class="layui-btn" id="Release" lay-filter="*" lay-submit>提交评论</button>
                </div>
                <div class="mod" style='float: right;'>
                    <input class='checkbox' type="checkbox" value='0' id="checkbox-11-1" /><label style='font-size: 62.5%;' for="checkbox-11-1"></label>
                    仅自己/楼主可见
                </div>
            </div>
        <script>
        $('#Release').click(function (e) {
            e.preventDefault();
            html = $('#editor_id_1').val();
            $.ajax({
                type: "POST",
                url: "/bbs/readarticle/comment_release",
                data: {
                    article_id: '$article_id',
                    comment_id: '$comment_id',
                    RandStr: '$RandStr',
                    comment: html,
                    me_info: '$me',
                    son:true,
                    checkbox:$('#checkbox-11-1').val(),
                },
                dataType: "json",
                success: function (response) {
                    if (response.code == 1) {
                        parent.$('#comment').append(response.msg);
			            parent.layer.close(index);
                    } else {
                        layer.tips('发生意外错误错题提示/信息' + response.msg + '请联系管理员', '#Release', {
                            tips: [1, '#3595CC'],
                            time: 4000
                        });
                        return;
                    }
                }
            });
        });
        $('.checkbox').click(function (e) {
            if($('#checkbox-11-1').val() == 2){
                $('#checkbox-11-1').val(0);
            }else{
                $('#checkbox-11-1').val(2);
            }
        });
        
        var editor;
        KindEditor.ready(function (K) {
            editor = K.create('#editor_id_1', {
                resizeType: 2,
                uploadJson: '{:url("upload/index/image_upload")}', // 相对于当前页面的路径
                newlineTag: 'br', //回车换行br|p
                width:"100%",
                afterBlur: function () {
                    editor.sync();
                    if (editor.html() == '<br />' || editor.html() == '') {
                        editor.html("$web_settings[bbs_readarticle_comment_text]");
                        $("#qstContent").contents().find(".ke-content").css("color", "red");
                    }
                },
                afterFocus: function () {
                    if (editor.html() == "$web_settings[bbs_readarticle_comment_text]") {
                        editor.html('');
                        // 输入文字变为黑色
                        $("#qstContent").contents().find(".ke-content").css("color", "#fff");
                    }
                },
            }); 
        });
        //注意：parent 是 JS 自带的全局对象，可用于操作父页面
		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        console.log(index);
		//让层自适应iframe
		$('#add').on('click', function () {
			$('body').append('插入很多酱油。插入很多酱油。插入很多酱油。插入很多酱油。插入很多酱油。插入很多酱油。插入很多酱油。');
			parent.layer.iframeAuto(index);
		});
		//在父层弹出一个层
		$('#new').on('click', function () {
			parent.layer.msg('Hi, man', {
				shade: 0.3
			})
		});
		//给父页面传值
		$('#transmit').on('click', function () {
			parent.$('#parentIframe').text('我被改变了');
			parent.layer.tips('Look here', '#parentIframe', {
				time: 5000
			});
			parent.layer.close(index);
		});
		//关闭iframe
		$('#closeIframe').click(function () {
			var val = $('#name').val();
			if (val === '') {
				parent.layer.msg('请填写标记');
				return;
			}
			parent.layer.msg('您将标记 [ ' + val + ' ] 成功传送给了父窗口');
			parent.layer.close(index);
		});
        </script>
ETO;
    }
  }
  //删除文章
  public function del_article()
  {
    $me = Session::get('bbs_user_info');
    if ($me) {
      $user_find = Db::table('user')
        ->where([
          'email' => $me['email'],
          'password' => $me['password'],
          'user_name' => $me['user_name'],
          'id' => $me['id'],
        ])
        ->find();
      if ($user_find) {
        $time = rsa_decode(input('time'));
        $ajaxTime = rsa_decode(array_keys(input())[1]); //获取post传入的时间时间戳 前段使用rsa加密
        if ($time == $ajaxTime && $time) {
          $id = rsa_decode(input(array_keys(input())[1])); // 文章id
          $article_info = Db::table('bbs_article')
            ->where(['id' => $id])
            ->field('user_id')
            ->find();
          if ($article_info && $article_info['user_id'] == $me['id']) {
            Db::table('bbs_article')
              ->where(['id' => $id])
              ->update(['start' => 1]);
            $Data['code'] = '0';
            $Data['ErrorPosition'] = 'null';
            $Data['start'] = 'success';
            return json($Data);
          } else {
            error("文章所属的用户不是本用户 本用户尝试删除文章 $id ", 2, 'del_article', true, $user_find);
            $Data['code'] = '10019';
            $Data['ErrorPosition'] = 'ReadarticleError';
            $Data['msg'] = '当前文章主人不是当前用户';
            return json($Data);
          }
        }
      } else {
        $Data['code'] = '10009';
        $Data['ErrorPosition'] = 'ReadarticleError';
        $Data['msg'] = 'UserInfoError - 请重新登录';
        return json($Data);
      }
    } else {
      $Data['code'] = '10009';
      $Data['ErrorPosition'] = 'ReadarticleError';
      $Data['msg'] = 'UserInfoError - 请重新登录';
      return json($Data);
    }
  }
}
